﻿using System;
using System.Collections.Generic;
using System.Xml.Serialization;
using System.Collections.Specialized;
using Vbyte.Configuration;

namespace Vbyte.DataSource.Cache
{
    /// <summary>
    /// 缓存配置
    /// </summary>
    [Serializable]
    public class CacheConfig
    {
        /// <summary>
        /// 缓存分钟数
        /// </summary>
        [XmlAttribute]
        public double CacheMinutes { get; set; }

        /// <summary>
        /// 实现接口BestTone.DataSource.Cache.ICachePolicy&lt;T, I&gt;的类型全称
        /// </summary>
        [XmlAttribute]
        public string ImpTypeFullName { get; set; }

        private CacheCompareMode _mode = CacheCompareMode.LocalCacheTime;

        /// <summary>
        /// 缓存比较模式
        /// </summary>
        [XmlAttribute]
        public CacheCompareMode Mode 
        {
            get { return _mode; }
            set { _mode = value; }
        }

        /// <summary>
        /// 特定类型的缓存配置
        /// </summary>
        [XmlElement(ElementName = "add")]
        public SpecialCache[] SpecialCaches { get; set; }

        private static CacheConfig _instance;
        /// <summary>
        /// 获取配置文件中的实例
        /// </summary>
        public static CacheConfig ConfigInstance
        {
            get
            {
                if (_instance == null)
                {
                    _instance = XmlSerializeSectionHandler.GetObject<CacheConfig>("CacheConfig");
                }
                return _instance;
            }
        }

    }

    /// <summary>
    /// 特定类型的缓存配置
    /// </summary>
    [Serializable]
    public class SpecialCache
    {
        /// <summary>
        /// 缓存实体类型名称
        /// </summary>
        [XmlAttribute]
        public string CacheType { get; set; }

        /// <summary>
        /// 缓存分钟数
        /// </summary>
        [XmlAttribute]
        public double CacheMinutes { get; set; }

        /// <summary>
        /// 缓存比较模式
        /// </summary>
        [XmlAttribute]
        public CacheCompareMode Mode { get; set; }

    }

    /// <summary>
    /// 缓存比较模式
    /// </summary>
    [Serializable]
    public enum CacheCompareMode:byte
    { 
        /// <summary>
        /// 本地缓存时间
        /// </summary>
        LocalCacheTime = 0,

        /// <summary>
        /// 数据更新时间
        /// </summary>
        RemoteUpdateTime = 1
    }

}
